<template>
  <div class="ai-page">
      <!-- 对话记录区域 -->
      <div class="chat-history">
          <div class="chat-message" v-for="(message, index) in chatHistory" :key="index">
              <div v-if="message.isUser" class="user-message">
                <div class="message-wrapper">
                  <div class="user-avatar">
                    <img src="@/assets/user-avatar.png" alt="用户头像">
                  </div>
                  <div class="message-bubble">
                    {{ message.content }}
                  </div>
                </div>
              </div>
              <div v-else class="ai-message">
                <div class="message-wrapper">
                  <div class="ai-avatar">
                    <img src="@/assets/ai-avatar.png" alt="AI头像">
                  </div>
                  <div class="message-bubble markdown-body" v-html="renderMarkdown(message.content)">
                  </div>
                </div>
              </div>
          </div>
      </div>
      <!-- 聊天输入区域 -->
      <div class="chat-input">
          <input 
            v-model="inputMessage" 
            type="text" 
            placeholder="请输入消息"
            @keyup.enter="sendMessage"
          >
          <button @click="sendMessage">发送</button>
      </div>
  </div>
</template>
  
<script>
import { ref, onMounted } from 'vue';
import { marked } from 'marked';
import { sendDeepSeekRequest } from '@/utils/sendDeepSeekRequest.ts';

export default {
  name: 'AiPage',
  setup() {
    const chatHistory = ref([]);
    const inputMessage = ref('');

    const renderMarkdown = (content) => {
      try {
        return marked(content);
      } catch (e) {
        return content;
      }
    };

    const sendMessage = async () => {
        if (!inputMessage.value) return;
        // 将用户消息添加到对话记录
        chatHistory.value.push({ isUser: true, content: inputMessage.value });
        inputMessage.value = '';

        const aiReply = await sendDeepSeekRequest(chatHistory.value);
        chatHistory.value.push({ isUser: false, content: aiReply });
    };

        // 在组件挂载时自动添加AI的打招呼消息
    onMounted(() => {
      chatHistory.value.push({
        isUser: false,
        content: '你好！我是AI助手，有什么可以帮您的吗？'
      });
    });

    return {
      chatHistory,
      inputMessage,
      renderMarkdown,
      sendMessage
    };
  },
  activated() {
    console.log('AiPage activated');
  },
  deactivated() {
    console.log('AiPage deactivated');
  }
}
</script>
  
<style scoped>
.ai-page {
  display: flex;
  flex-direction: column;
  height: 88vh;
  background-color: #f5f5f5;
}

.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.chat-message {
  margin-bottom: 20px;
}

.message-wrapper {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.user-message .message-wrapper {
  flex-direction: row-reverse;
}

.user-avatar img,
.ai-avatar img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.message-bubble {
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
}

.user-message .message-bubble {
  background-color: #007AFF;
  color: white;
  border-top-right-radius: 4px;
}

.ai-message .message-bubble {
  background-color: white;
  color: #333;
  border-top-left-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Markdown 样式 */
.markdown-body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin-top: 16px;
  margin-bottom: 16px;
  font-weight: 600;
  line-height: 1.25;
}

.markdown-body code {
  padding: 0.2em 0.4em;
  margin: 0;
  font-size: 85%;
  background-color: rgba(27, 31, 35, 0.05);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
}

.markdown-body pre {
  padding: 16px;
  overflow: auto;
  font-size: 85%;
  line-height: 1.45;
  background-color: #f6f8fa;
  border-radius: 6px;
  margin-bottom: 16px;
}

.markdown-body pre code {
  padding: 0;
  margin: 0;
  font-size: 100%;
  word-break: normal;
  white-space: pre;
  background: transparent;
  border: 0;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 2em;
  margin-top: 0;
  margin-bottom: 16px;
}

.markdown-body li {
  margin-top: 0.25em;
}

.markdown-body p {
  margin-top: 0;
  margin-bottom: 16px;
}

.markdown-body blockquote {
  padding: 0 1em;
  color: #6a737d;
  border-left: 0.25em solid #dfe2e5;
  margin: 0 0 16px 0;
}

.chat-input {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: white;
  border-top: 1px solid #e0e0e0;
  gap: 12px;
}

.chat-input input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid #e0e0e0;
  border-radius: 24px;
  font-size: 14px;
  background-color: #f8f8f8;
  transition: all 0.3s ease;
}

.chat-input input:focus {
  border-color: #007AFF;
  background-color: white;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.1);
}

.chat-input button {
  padding: 10px 24px;
  background-color: #007AFF;
  color: white;
  border: none;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.chat-input button:hover {
  background-color: #0056b3;
}

.chat-input input::placeholder {
  color: #999;
}
</style>